<?php
class Productcategory_model extends Model{

	var $_datagrid_name = 'dt1';
	
    function Productcategory_model(){
        parent::Model();       
    }
	function getdatasource(){ 
    	$page = $this->input->post($this->_datagrid_name.'_hidden_page')?$this->input->post($this->_datagrid_name.'_hidden_page'):1;    	
    	$limit = $this->config->item('record_limit'); 
    	$this->db->where(array('superid !='=>0));
    	$this->db->from('productcategory');	
    	$count = $this->db->count_all_results();    	
    	$pages = intval($count/$limit) + ($count%$limit?1:0);
    	$page = $page>$pages?1:$page;
    	$this->db->limit($limit,($page-1)*$limit);
    	$this->db->select('pc1.id,pc1.superid,pc1.sortorder');
    	$this->db->join('productcategory pc2', 'pc2.id = pc1.superid');
    	$this->db->order_by('pc2.sortorder,pc1.sortorder,pc1.id');    	
    	$this->db->where(array('pc1.superid !='=>0));
    	$query = $this->db->get('productcategory pc1');    	
    	$dts = $query->result_rowlist();
    	for($i=0;$i<$query->num_rows;$i++){
    		$dts[$i][3]=$dts[$i][2];
    		$dts[$i][2]=get_content('productcategory',$dts[$i][0])->categoryname;
    		$dts[$i][1]=get_content('productcategory',$dts[$i][1])->categoryname;    		
    	}
    	$datagrid->id = $this->_datagrid_name;
    	$datagrid->page = $page;
    	$datagrid->pages = $pages;
    	$datagrid->datasource = $dts;    	
    	return $datagrid;    	
    }
	function delete(){
    	if($this->input->post($this->_datagrid_name.'_hidden_value')){
    		$id = $this->input->post($this->_datagrid_name.'_hidden_value');
    		delete_content('productcategory',$id);
    		$this->db->delete('productcategory',array('id'=>$id));
    	}
    	else if($this->input->post($this->_datagrid_name.'_chk_select')){
    		$id = $this->input->post($this->_datagrid_name.'_chk_select');
    		for($i=0;$i<count($id);$i++){
    			delete_content('productcategory',$id[$i]);
	    		$this->db->delete('productcategory',array('id'=>$id[$i]));
    		}
    	}
    }
    function addnew(){
    	if(FALSE!==$this->input->post('hidden_status')){
    		$this->db->insert('productcategory',array('superid'=>$this->input->post('cbo_location'),'sortorder'=>0));
    		set_content('productcategory',$this->db->insert_id(),array('categoryname'=>$this->input->post('txt_categoryname')));
    		redirect('productcategory','location');
    	}
    }
    function update(){
    	if(FALSE!==$this->input->post('hidden_status')){
    		$this->db->where('id',$this->input->post('hidden_id'));
    		$this->db->update('productcategory',array('superid'=>$this->input->post('cbo_location')));
    		set_content('productcategory',$this->input->post('hidden_id'),array('categoryname'=>$this->input->post('txt_categoryname')));
    		redirect('productcategory','location');
    	}
    }
    function getdetail($id){
    	$query = $this->db->get_where('productcategory',array('id'=>$id));
    	if($query->num_rows){
    		$detail->id = $id;
    		$detail->locationid = $query->row()->superid;
	    	$detail->categoryname = get_content('productcategory',$id)->categoryname;
	    	return $detail;
    	}
    	else return FALSE;
    }
    function save(){
    	if(FALSE!==$this->input->post('hidden_status')){
    		$id = $this->input->post($this->_datagrid_name.'_hidden_id');
    		$sortorder = $this->input->post($this->_datagrid_name.'_column_3');
    		for($i=0;$i<count($id);$i++){
    			$this->db->where('id',$id[$i]);
    			$this->db->update('productcategory',array('sortorder'=>$sortorder[$i]));
    		}    		
    	}
    }
    function getlocationlist(){
    	$this->db->select('id');
    	$this->db->order_by('sortorder');
    	$query = $this->db->get_where('productcategory',array('superid'=>0));
    	$data = $query->result();
    	for($i=0;$i<$query->num_rows;$i++){
    		$data[$i]->name=get_content('productcategory',$data[$i]->id)->categoryname;
    	}
    	return $data;
    }
}

/* End of file productcategory_model.php */
/* Location: ./system/application/admin/controllers/productcategory_model.php */